package com.jf.cloud.live.controller.platform;

import com.jf.cloud.api.product.feign.SpuFeignClient;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.live.param.LiveUsableNumParam;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.dto.LiveProdStoreDTO;
import com.jf.cloud.live.model.LiveProdStore;
import com.jf.cloud.live.service.LiveLogService;
import com.jf.cloud.live.service.LiveProdStoreService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 直播商品
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@RestController("platformLiveProdStoreController")
@RequestMapping("/p/live_prod_store")
@Tag(name = "平台端直播间商品库接口")
public class LiveProdStoreController {

    @Autowired
    private LiveProdStoreService liveProdStoreService;
    @Autowired
    private LiveLogService liveLogService;
    @Autowired
    private SpuFeignClient spuFeignClient;

    @GetMapping("/page")
    @Operation(summary = "分页查找直播间商品库信息")
    public ServerResponseEntity<PageVO<LiveProdStore>> page(@Valid PageDTO pageDTO, @ParameterObject LiveProdStoreDTO liveProdStoreDTO) {
        PageVO<LiveProdStore> liveProdStorePage = liveProdStoreService.page(pageDTO, liveProdStoreDTO);
        return ServerResponseEntity.success(liveProdStorePage);
    }


    @GetMapping("/info/{liveProdStoreId}")
    @Operation(summary = "通过id查询直播间商品库")
    @Parameter(name = "liveProdStoreId", description = "直播间商品库id" , required = true)
    public ServerResponseEntity<LiveProdStore> getById(@ParameterObject @PathVariable("liveProdStoreId") Long liveProdStoreId) {
        LiveProdStore liveProdStore = liveProdStoreService.getByLiveProdStoreId(liveProdStoreId);
        if (Objects.nonNull(liveProdStore.getProdId())) {
            ServerResponseEntity<SpuVO> spuResponse = spuFeignClient.getById(liveProdStore.getProdId());
            SpuVO spu = spuResponse.getData();
            if (spuResponse.isSuccess() && spu != null) {
                liveProdStore.setCoverPic(spu.getMainImgUrl());
                liveProdStore.setName(spu.getName());
                liveProdStore.setOriProdType(spu.getSpuType());
            }
        }
        return ServerResponseEntity.success(liveProdStore);
    }

    @DeleteMapping("/{liveProdStoreId}")
    @Operation(summary = "通过id删除直播间商品库")
    @Parameter(name = "liveProdStoreId", description = "直播间商品库id" , required = true)
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long liveProdStoreId) throws WxErrorException {
        liveProdStoreService.removeWxLiveProdById(liveProdStoreId, Constant.PLATFORM_SHOP_ID);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getAddProdNum")
    @Operation(summary = "查询商家今日剩余可用添加直播商品、可删除商品库商品次数")
    public ServerResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.ADD_PROD_AUDIT);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.DELETE_PROD);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ServerResponseEntity.success(liveNum);
    }
}
